package cn.icanci.loopstack.bic.biz.service.impl;

import java.util.List;

import javax.annotation.Resource;

import org.springframework.stereotype.Service;

import cn.icanci.loopstack.bic.biz.cache.GraphRepository;
import cn.icanci.loopstack.bic.biz.mapper.UnitViewSnapshotMapper;
import cn.icanci.loopstack.bic.biz.service.UnitViewSnapshotService;
import cn.icanci.loopstack.bic.common.model.snapshot.UnitViewSnapshotVO;
import cn.icanci.loopstack.bic.dal.mongo.common.PageList;
import cn.icanci.loopstack.bic.dal.mongo.daointerface.UnitViewSnapshotDAO;
import cn.icanci.loopstack.bic.dal.mongo.dateobject.UnitViewSnapshotDO;

/**
 * @author icanci
 * @since 1.0 Created in 2023/09/20 21:45
 */
@Service
public class UnitViewSnapshotServiceImpl implements UnitViewSnapshotService {

    @Resource
    private UnitViewSnapshotDAO    unitViewSnapshotDAO;
    @Resource
    private UnitViewSnapshotMapper unitViewSnapshotMapper;
    @Resource
    private GraphRepository        graphRepository;

    @Override
    public List<UnitViewSnapshotVO> queryAll() {
        return unitViewSnapshotMapper.dos2vos(unitViewSnapshotDAO.queryAll());
    }

    @Override
    public void save(UnitViewSnapshotVO unitViewSnapshotVO) {
        // TODO 日志记录
        if (doInsert(unitViewSnapshotVO)) {
            UnitViewSnapshotDO insert = unitViewSnapshotMapper.vo2do(unitViewSnapshotVO);
            unitViewSnapshotDAO.insert(insert);
        } else {
            unitViewSnapshotDAO.update(unitViewSnapshotMapper.vo2do(unitViewSnapshotVO));
        }
        graphRepository.refresh(unitViewSnapshotVO.getSystemUuid(), unitViewSnapshotVO.getUseCaseUuid());
    }

    @Override
    public UnitViewSnapshotVO queryById(String id) {
        return unitViewSnapshotMapper.do2vo(unitViewSnapshotDAO.queryOneById(id));
    }

    @Override
    public PageList<UnitViewSnapshotVO> queryPage(UnitViewSnapshotVO unitViewSnapshotVO, int pageNum, int pageSize) {
        PageList<UnitViewSnapshotDO> pageQuery = unitViewSnapshotDAO.pageQuery(unitViewSnapshotMapper.vo2do(unitViewSnapshotVO), pageNum, pageSize);
        return new PageList<>(unitViewSnapshotMapper.dos2vos(pageQuery.getData()), pageQuery.getPaginator());

    }

    @Override
    public UnitViewSnapshotVO queryBySystemUuidAndUseCaseUuidAndVersion(String systemUuid, String useCaseUuid, long version) {
        return unitViewSnapshotMapper.do2vo(unitViewSnapshotDAO.queryBySystemUuidAndUseCaseUuidAndVersion(systemUuid, useCaseUuid, version));
    }
}
